package me.pplay.playtalk.view;

import java.util.ArrayList;
import java.util.List;

import me.pplay.playtalk.R;
import me.pplay.playtalk.activity.ChatConversationActivity;
import me.pplay.playtalk.adapter.FaceAdapter;
import me.pplay.playtalk.adapter.MyPagerAdapter;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

public class PagerLayout extends FrameLayout {
	private Context mContext;
	private int index		 		= 0;
	private int currentIndex 		= 0;
	private int totalPagerCount		= 0;
	private List<String> faces;
	private List<View> mViews;
	private List<FaceAdapter> adapters;
	private List<ImageView> pagePointViewList;
	private final int dispalyCount	= 5;
	private final int dispalyTotal  = 3 * dispalyCount;
	
	private Handler	mHandler;
	
	private ViewPager mViewPager;
	private LinearLayout pageControlLayout;
	
	public PagerLayout(Context context) {
		super(context);
		mContext		= context;
		addViews();
	}
	
	public PagerLayout(Context context, int index, List<String> faces, Handler handler) {
		super(context);
		mContext		= context;
		this.faces		= faces;
		this.index		= index;
		System.out.println("ethan smile::index:"+index);
		this.mHandler	= handler;
		totalPagerCount	= faces.size() / dispalyTotal;
		if(faces.size() % dispalyTotal != 0)totalPagerCount++;
		addViews();
	}

	public PagerLayout(Context context,AttributeSet attrs) {
		super(context, attrs);
	}
	
	private void addViews(){
		View v				= LayoutInflater.from(mContext).inflate(R.layout.layout_pager, null);
		mViewPager			= (ViewPager)v.findViewById(R.id.pager);
		pageControlLayout	= (LinearLayout)v.findViewById(R.id.pageControlLayout);
		this.addView(v, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		iniController();
		iniListener();
	}
	
	private void iniController(){
		mViews				= new ArrayList<View>();
		adapters			= new ArrayList<FaceAdapter>();
		pagePointViewList	= new ArrayList<ImageView>();
		for(int i = 0; i < totalPagerCount; i++){
			GridView gridView 	= getGridView();
			FaceAdapter	adapter = null;
			if(i == 0){
				adapter = new FaceAdapter(mContext, index, getFaces());
				gridView.setAdapter(adapter);
			}
			gridView.setOnItemClickListener(new GridViewOnItemClickListener());
			adapters.add(adapter);
			mViews.add(gridView);
		}
		mViewPager.setAdapter(new MyPagerAdapter(mViews));
		addPagePoint();
	}
	
	
	private void iniListener(){
		mViewPager.setOnPageChangeListener(new MyPagerOnPageChangeListener());
	}
	
	private String[] getFaces(){
		int startIndex	= currentIndex * dispalyTotal;
		int endIndex 	= (currentIndex + 1)* dispalyTotal;
		if(endIndex > faces.size())
			endIndex = faces.size();
		String[] facesItem = new String[endIndex - startIndex];
		for (int i = startIndex; i < endIndex; i++) {
			facesItem[i-startIndex] = faces.get(i);
		}
		return facesItem;
	}
	
	private GridView getGridView(){
		GridView gridView = new GridView(mContext);
//		gridView.setBackgroundColor(Color.RED);
		gridView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		gridView.setVerticalSpacing(5);
		gridView.setHorizontalSpacing(5);
		gridView.setPadding(0, 10, 0, 0);
		gridView.setNumColumns(dispalyCount);
		return gridView;
	}
	
	/**
	 * init page point
	 */
	private void addPagePoint() {
		for (int i = 0; i < totalPagerCount; i++) {
			ImageView imageView = new ImageView(mContext);
			imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			imageView.setPadding(10, 0, 10, 10);
			imageView.setScaleType(ScaleType.CENTER);
			setPointImageResource(imageView, i);
			pagePointViewList.add(imageView);
			pageControlLayout.addView(imageView);
		}
	}

	/**
	 * change page point
	 */
	private void changePointImageResource() {
		for (int i = 0; i < totalPagerCount; i++) {
			setPointImageResource(pagePointViewList.get(i), i);
		}
	}

	private void setPointImageResource(ImageView imageView, int indexPoint) {
		if (indexPoint == currentIndex)
			imageView.setImageResource(R.drawable.pagepoint1);
		else
			imageView.setImageResource(R.drawable.pagepoint2);
	}

	
	
	class GridViewOnItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
			Message msg = new Message();
			msg.what	= ChatConversationActivity.SMILEY_TEXT;
			msg.obj		= faces.get(position + currentIndex * dispalyTotal);
//			msg.obj		= face.faceNamesList.get(index)[position + currentIndex * dispalyTotal];//face.faceMap.getfaces.get(columnList[currentIndex]).get(position);
			msg.arg1	= index;
			System.out.println("ethan smile selected:"+msg.obj + "   :"+msg.arg1);
			mHandler.sendMessage(msg);		
		}
	}
	
	private class MyPagerOnPageChangeListener implements OnPageChangeListener{

		@Override
		public void onPageScrollStateChanged(int arg0) {}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {}
		
		@Override
		public void onPageSelected(int position) {
			currentIndex = position;
			if(adapters.size() > position){
				if(adapters.get(position) == null){
					FaceAdapter adapter = new FaceAdapter(mContext, index, getFaces());
					((GridView)mViews.get(position)).setAdapter(adapter);
					adapters.add(adapter);
				}
			}
			mViewPager.setCurrentItem(currentIndex);
			changePointImageResource();
		}
	}
}
